package com.sudoctor.util;

import java.lang.reflect.Field;


public class Transformer {
	
	public <T, K> void transformerBeanToForm(T form, K bean) {
		Class classDto = form.getClass();
		Class classBean = bean.getClass();
		Field[] dtoFields = classDto.getDeclaredFields();
		Field[] beanFields = classBean.getDeclaredFields();
		try {
			uno: for (Field dt : dtoFields) {
				String dtoField = dt.getName();
				for (Field s : beanFields) {
					if (dtoField.equals(s.getName())) {
						dt.setAccessible(true);
						s.setAccessible(true);
						dt.set(form, s.get(bean));
						continue uno;
					}
				}
			}

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}


	public <T, K> void transformerFormToBean(T form, K bean) {
		Class classDto = form.getClass();
		Class classBean = bean.getClass();
		Field[] dtoFields = classDto.getDeclaredFields();
		Field[] beanFields = classBean.getDeclaredFields();
		try {
			uno: for (Field dt : dtoFields) {
				String dtoField = dt.getName();
				for (Field s : beanFields) {
					if (dtoField.equals(s.getName())) {
						dt.setAccessible(true);
						s.setAccessible(true);
						s.set(bean, dt.get(form));
						continue uno;
					}
				}
			}

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}

	}
	
}

